<?php


namespace app;


use epii\admin\center\admin_center_controller;
use epii\admin\ui\lib\epiiadmin\jscmd\Alert;
use epii\admin\ui\lib\epiiadmin\jscmd\Close;
use epii\admin\ui\lib\epiiadmin\jscmd\CloseAndRefresh;
use epii\admin\ui\lib\epiiadmin\jscmd\JsCmd;
use epii\admin\ui\lib\epiiadmin\jscmd\Refresh;
use epii\server\Args;
use libs\Tool;
use model\AdminModel;
use service\admin\HospitalService;
use think\Db;
use HPlus\Validate\Validate;
use wangshouwei\session\Session;

class base extends admin_center_controller
{
    protected $adminInfo=[];
    protected $admin_allow=['index','ajax_data'];
    public function init()
    {
        $this->adminInfo = AdminModel::getInfo(['id'=>Session::get('user_id')]);
        if($this->adminInfo['hospital_id']){
            //获取医院分组id
            $this->adminInfo['hospital_group_id'] = HospitalService::getGroupId($this->adminInfo['hospital_id']);
        }else{
            $this->adminInfo['hospital_group_id'] =0;
        }
        $this->assign('adminInfo',$this->adminInfo);
        $this->assign('laydate',Tool::getDomain().'/static/laydate/');
        parent::init();

    }
    /**
     * 功能：成功响应
     * Created at 2021/2/23 16:35 by 陈庙琴
     * @param string $msg
     * @param string $sufAction
     * @return array|false|string
     */
    public function success($msg = '成功', $sufAction = 'close_and_refresh')
    {
        if ($sufAction === 'close') {
            $action = Close::make();
        } else if ($sufAction === 'refresh') {
            $action = Refresh::make();
        } else {
            $action = CloseAndRefresh::make();
        }
        $cmd = Alert::make()->icon('6')->msg($msg)->onOk($action);

        exit(JsCmd::make()->addCmd($cmd)->run());
    }

    /**
     * 功能：失败响应
     * Created at 2021/2/23 16:35 by 陈庙琴
     * @param string $msg
     * @return array|false|string
     */
    public function error($msg = '失败')
    {
        $cmd = Alert::make()->icon('5')->msg($msg)->onOk(null);
        exit(JsCmd::make()->addCmd($cmd)->run());
    }
    protected function validate(array $data, $validate, array $message = [], bool $batch = false,$return_json=false)
    {

        $v = new Validate();
        $v->rule($validate);
        $v->message($message);
        // 是否批量验证
        if ($batch) {
            $v->batch(true);
        }
        $check=$v->check($data);
        if(!$check){
           $return_json?$this->jsonError($v->getError()):$this->error($v->getError());
        }
        return $check;

    }
    //json 成功返回
    protected function jsonSuccess($msg='成功',$data=''){
        exit(json_encode(Tool::Success($msg,$data)));
    }
    //json 失败返回
    protected function jsonError($msg='失败',$data=''){
        exit(json_encode(Tool::Error($msg,$data)));
    }
    //禁止总管理员访问
    protected function accessDenied(){
        $method=explode('@',Args::params('app'));
        $method=end($method);
//        var_dump($this->adminInfo['hospital_id']);
//        var_dump(in_array($method,$this->admin_allow));
        if(!$this->adminInfo['hospital_id']&&!in_array($method,$this->admin_allow)){
            echo "<script>alert('没有访问权限');</script>";
            exit;
        }
    }

}